﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;

namespace SQLServerDAL
{
    public class CardDAL : ICardDAL
    {
        public Boolean InsertCard(Card card)
        {
            try
            {
                
                CardDataClassesDataContext cardContext = new CardDataClassesDataContext();
                cardContext.Cards.InsertOnSubmit(card);
                cardContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteCard(int cardId)
        {
            try
            {
                CardDataClassesDataContext cardContext = new CardDataClassesDataContext();
                Card card = (from u in cardContext.Cards
                             where u.CardId.Equals(cardId)
                             select u).First();
                cardContext.Cards.DeleteOnSubmit(card);
                cardContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean UpdateCard(int cardId, Card card)
        {

            if (cardId != card.CardId)
            {
                return false;
            }
            CardDataClassesDataContext cardContext = new CardDataClassesDataContext();
            Card query = (from u in cardContext.Cards
                          where u.CardId.Equals(cardId)
                          select u).First();
            //query.Password = card.Password;

            cardContext.SubmitChanges();
            return true;

        }

        public Card SelectCardById(int cardId)
        {
            CardDataClassesDataContext cardContext = new CardDataClassesDataContext();
            Card card = (from u in cardContext.Cards
                         where u.CardId.Equals(cardId)
                         select u).First();
            return card;
        }


        public List<Card> SelectAllCard()
        {
            CardDataClassesDataContext cardContext = new CardDataClassesDataContext();
            var query = (from u in cardContext.Cards
                         select u);
            List<Card> cardList = new List<Card>();

            foreach (var card in query)
            {
                cardList.Add(card);
            }
            return cardList;
        }
    }
}
